home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / Z80ppc 160.sit / Z80ppc 160 / Z80.ppc.macros < prev    next >
Text File  |  1995-11-01  |  8KB  |  330 lines

  1. /*    Z80 Emulator: macro definitions, opcode enumerations
  2.     Copyright (C) 1995 G.Woigk
  3.     
  4.     This file is part of Mac Spectacle and it is free software
  5.     See application.c for details
  6.             
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  10.  
  11.     based on fMSX; Copyright (C) Marat Fayzullin 1994,1995
  12. */
  13.  
  14.  
  15. // ----    bit masks for z80 flag register -------------------------------
  16. #define S_FLAG    0x80
  17. #define Z_FLAG    0x40
  18. #define H_FLAG    0x10
  19. #define P_FLAG    0x04
  20. #define V_FLAG    0x04
  21. #define N_FLAG    0x02
  22. #define C_FLAG    0x01
  23.  
  24.  
  25. // ----    read/write/modify memory --------------------------------------
  26. #define    core(A)        CORE[(Short)(A)]            // memory access for read-modify-write
  27. #define    peek(A)        core(A)                        // read byte
  28. #define    poke(A,N)    core(A)=N                    // write byte
  29. #define    poke2(A,N)    poke(A,N),poke(A+1,N>>8)    // write word ((A and N must be constant))
  30.  
  31. #define    n            peek(pc++)                    // get next byte via pc
  32. #define    nn            (wm=n,wm+((Short)n<<8))        // get next word via pc
  33. #define    dis            (char)n                        // get signed char via pc
  34.  
  35. #define    pop()        (wm=peek(sp++),wm+((Short)peek(sp++)<<8))
  36. #define    push(N)        poke(--sp,N>>8), poke(--sp,N)
  37.  
  38.  
  39. // ----    write to RAM with ROM protection -------------------------------
  40. //        use as last instruction only!
  41. //        Address parameter must be constant
  42. //        Word parameter must be constant
  43. //        Byte parameter may be a calculated expression like (iz+dis)
  44. //        note: rompoke2 may overwrite first byte of ROM and may call write_to_rom() for first byte of RAM
  45.  
  46. #define    in_ram(A)        (A>=0x4000)
  47. #define    in_rom(A)        (A<0x4000)
  48. #define    rompoke(A,N)    if (in_ram(A)) { poke(A,N); loop; } write_to_rom(A,N)
  49. #define    rompoke2(A,N)    if (in_ram(A)) { poke2(A,N);loop; } write_to_rom(A,N); write_to_rom(A+1,N>>8)
  50.  
  51.  
  52.  
  53. // ----    Counting individual instructions and executed locations --------------------
  54.  
  55. #if PC_PROFILE
  56. #define    COUNT_PC            { if (count_pc) if (!++cnt_pc[pc]) Z80_1st_Loc(cc,pc); }
  57. #else
  58. #define    COUNT_PC
  59. #endif
  60.  
  61. #if CMD_PROFILE
  62. #define    COUNT_INSTR            { if (count_instr) if (!++cnt_xx[peek(pc)] )  Z80_1st_Instr(cc,pc); }
  63. #define    COUNT_CB_INSTR        { if (count_instr) if (!++cnt_cb[peek(pc)] )  Z80_1st_Instr(cc,pc); }
  64. #define    COUNT_ED_INSTR        { if (count_instr) if (!++cnt_ed[peek(pc)] )  Z80_1st_Instr(cc,pc); }
  65. #define    COUNT_XY_INSTR        { if (count_instr) if (!++cnt_xy[peek(pc)] )  Z80_1st_Instr(cc,pc); }
  66. #define    COUNT_XYCB_INSTR    { if (count_instr) if (!++cnt_xycb[peek(pc)]) Z80_1st_Instr(cc,pc); }
  67. #else
  68. #define    COUNT_INSTR
  69. #define    COUNT_CB_INSTR
  70. #define    COUNT_ED_INSTR
  71. #define    COUNT_XY_INSTR
  72. #define    COUNT_XYCB_INSTR
  73. #endif
  74.  
  75.  
  76. // ----    Macros which call Z80_Info(cc,ip) or do nothing ---------------------------------
  77. //        depending on flags defined in z80.options
  78.  
  79.  
  80. #if INFO_IRPT
  81. #define    do_info_irpt    Z80_Info_Irpt ( cc )
  82. #else
  83. #define    do_info_irpt    
  84. #endif
  85.  
  86.  
  87. #if INFO_NMI
  88. #define    do_info_nmi        Z80_Info_NMI ( cc )
  89. #else
  90. #define    do_info_nmi    
  91. #endif
  92.  
  93.  
  94. #if INFO_ILLEGALS
  95. #define    loop_ill2        goto ill2
  96. #define    loop_ill3        goto ill3
  97. #define    loop_ill4        goto ill4
  98. #else
  99. #define    loop_ill2        loop
  100. #define    loop_ill3        loop
  101. #define    loop_ill4        loop
  102. #endif
  103.  
  104.  
  105. #if INFO_WEIRD
  106. #define    loop_weird1        goto weird1
  107. #define    loop_weird2        goto weird2
  108. #define    loop_weird3        goto weird3
  109. #define    loop_weird4        goto weird4
  110. #else
  111. #define    loop_weird1        loop
  112. #define    loop_weird2        loop
  113. #define    loop_weird3        loop
  114. #define    loop_weird4        loop
  115. #endif
  116.  
  117.  
  118. #if INFO_MISC
  119. #define    info_misc1        Z80_Info( cc, pc-1 )
  120. #define    info_misc2        Z80_Info( cc, pc-2 )
  121. #else
  122. #define    info_misc1
  123. #define    info_misc2
  124. #endif
  125.  
  126.  
  127. // -----    Exact register R handling:  R is kept in register
  128. #if EXACT_R    
  129. #define    increment_r    r++
  130. #define    decrement_r    r--
  131. #define    add_r(N)    r += N
  132. #define    store_r        RR = (RR&0x80) + (r&0x7F)
  133. #define    load_r        r = RR
  134. #define    ld_r_a        RR = r = ra
  135. #define    ld_a_r        ra = (RR&0x80) + (r&0x7F)
  136.  
  137. // -----    Use register R for random generation only: Bit 7 is kept in RR
  138. #else
  139. #define    increment_r
  140. #define    decrement_r
  141. #define    add_r(N)
  142. #define    store_r
  143. #define    load_r
  144. #define    ld_r_a        RR = ra
  145. #define    ld_a_r        ra    = (RR&0x80) + (Random()&0x7F)
  146. #endif
  147.  
  148.  
  149. // --------------------------------------------------------------------
  150. // ----    INSTRUCTION MACROS --------------------------------------------
  151. // --------------------------------------------------------------------
  152.  
  153. /*    RLC ... SRL:    set/clr C, Z, P, S;
  154.                     clear    N=0, H=0
  155.                     pres.    none
  156. */
  157. #define M_RLC(R)                      ¥
  158.     rf  = R>>7;                        ¥
  159.     R   = (R<<1)+rf;                 ¥
  160.     rf |= zlog_flags[R]
  161.  
  162. #define M_RRC(R)                      ¥
  163.     rf  = R&0x01;                    ¥
  164.     R   = (R>>1)+(rf<<7);             ¥
  165.     rf |= zlog_flags[R]
  166.  
  167. #define M_RL(R)                        ¥
  168.     if (R&0x80)                        ¥
  169.     {    R     = (R<<1)+(rf&0x01);        ¥
  170.         rf    = zlog_flags[R]+C_FLAG;    ¥
  171.     } else                            ¥
  172.     {    R     = (R<<1)+(rf&0x01);        ¥
  173.         rf    = zlog_flags[R];        ¥
  174.     }
  175.  
  176. #define M_RR(R)                        ¥
  177.     if (R&0x01)                        ¥
  178.     {    R     = (R>>1)+(rf<<7);        ¥
  179.         rf    = zlog_flags[R]+C_FLAG;    ¥
  180.     } else                            ¥
  181.     {    R     = (R>>1)+(rf<<7);        ¥
  182.         rf    = zlog_flags[R];        ¥
  183.     }
  184.   
  185. #define M_SLA(R)                    ¥
  186.     rf    = R>>7;                        ¥
  187.     R <<= 1;                        ¥
  188.     rf |= zlog_flags[R]
  189.  
  190. #define M_SRA(R)                    ¥
  191.     rf    = R&0x01;                    ¥
  192.     R    = (R&0x80)+(R>>1);            ¥
  193.     rf |= zlog_flags[R]
  194.  
  195. #define M_SLL(R)                    ¥
  196.     rf    = R>>7;                        ¥
  197.     R    = (R<<1)+1;                    ¥
  198.     rf |= zlog_flags[R]
  199.  
  200. #define M_SRL(R)                    ¥
  201.     rf    = R&0x01;                    ¥
  202.     R >>= 1;                        ¥
  203.     rf |= zlog_flags[R]
  204.  
  205.  
  206. /*    BIT:    set/clr    Z
  207.             clear    N=0, H=1
  208.             pres    C
  209.             takes other flags from corresponding bits in tested byte!
  210. */
  211. #define M_BIT(N,R)    rf    = (rf&C_FLAG) + (R&(S_FLAG+P_FLAG)) + H_FLAG + ((R&N)?0:Z_FLAG)
  212.  
  213.  
  214. /*    ADD ... CP:    set/clr    Z, S, V, C, N, H
  215.                 pres    none
  216. */
  217. #define M_ADD(R)                                ¥
  218.     wm    = ra+R;                                    ¥
  219.     rf    = wmh + (wml?0:Z_FLAG) + (wml&S_FLAG)    ¥
  220.              + (~(ra^R)&(wml^ra)&0x80?V_FLAG:0)    ¥
  221.              + ((ra^R^wml)&H_FLAG);                ¥
  222.     ra    = wml
  223.  
  224. #define M_SUB(R)                                ¥
  225.     wm    = ra-R;                                    ¥
  226.     rf    = -wmh + (wml?0:Z_FLAG) + (wml&S_FLAG)    ¥
  227.               + ((ra^R)&(wml^ra)&0x80?V_FLAG:0)    ¥
  228.               + ((ra^R^wml)&H_FLAG) + N_FLAG;    ¥
  229.     ra    = wml
  230.  
  231. #define M_ADC(R)                                ¥
  232.     wm    = ra+R+(rf&C_FLAG);                        ¥
  233.     rf    = wmh + (wml?0:Z_FLAG) + (wml&S_FLAG)    ¥
  234.              + (~(ra^R)&(wml^ra)&0x80?V_FLAG:0)    ¥
  235.              + ((ra^R^wml)&H_FLAG);                ¥
  236.     ra    = wml
  237.  
  238. #define M_SBC(R)                                ¥
  239.     wm    = ra-R-(rf&C_FLAG);                        ¥
  240.     rf    = -wmh + (wml?0:Z_FLAG) + (wml&S_FLAG)    ¥
  241.               + ((ra^R)&(wml^ra)&0x80?V_FLAG:0)    ¥
  242.               + ((ra^R^wml)&H_FLAG) + N_FLAG;    ¥
  243.     ra    = wml
  244.  
  245. #define M_CP(R)                                    ¥
  246.     wm    = ra-R;                                    ¥
  247.     rf    = -wmh + (wml?0:Z_FLAG) + (wml&S_FLAG)    ¥
  248.               + ((ra^R)&(wml^ra)&0x80?V_FLAG:0)    ¥
  249.               + ((ra^R^wml)&H_FLAG) + N_FLAG;
  250.  
  251.  
  252. /*    AND ... XOR:    set/clr    Z, P, S
  253.                     clear    C=0, N=0, H=0/1 (OR,XOR/AND)
  254.                     pres    none
  255. */
  256. #define M_AND(R)            ¥
  257.     ra &= R;                ¥
  258.     rf    = H_FLAG|zlog_flags[ra]
  259.  
  260. #define M_OR(R)                ¥
  261.     ra |= R;                ¥
  262.     rf    = zlog_flags[ra]
  263.  
  264. #define M_XOR(R)            ¥
  265.     ra ^= R;                ¥
  266.     rf    = zlog_flags[ra]
  267.  
  268.  
  269. /*    INC ... DEC:    set/clr    Z,P,S,H
  270.                     clear    N=0/1 (INC/DEC)
  271.                     pres    C
  272. */
  273. #define M_INC(R)            ¥
  274.     R++;                    ¥
  275.     rf    = (rf&C_FLAG) + (R?0:Z_FLAG) + (R&S_FLAG) + (R==0x80?V_FLAG:0) + (R&0x0F?0:H_FLAG)
  276.  
  277. #define M_DEC(R)            ¥
  278.     R--;                    ¥
  279.     rf    = (rf&C_FLAG) + (R?0:Z_FLAG) + (R&S_FLAG) + (R==0x7F?V_FLAG:0) + (((R+1)&0x0F)?0:H_FLAG) + N_FLAG
  280.  
  281.  
  282. /*    ADDW:    set/clr    C
  283.             clear    N=0
  284.             pres    Z, P, S
  285.             unkn    H
  286. */
  287. #define M_ADDW(R1,R2)                            ¥
  288.     rf &= ~(N_FLAG+C_FLAG);                        ¥
  289.     rf |= ((Long)R1+(Long)R2)>>16;                ¥
  290.     R1 += R2;
  291.  
  292.  
  293. /*    ADCW, SBCW:    set/clr    C,Z,V,S
  294.                 clear    N=0/1 (ADC/SBC)
  295.                 unkn    H
  296.                 pres    none
  297. */
  298. #define M_ADCW(R)                                ¥
  299.     wm    = HL+R+(rf&C_FLAG);                        ¥
  300.     rf    = (((Long)HL+(Long)R+(rf&C_FLAG))>>16)    ¥
  301.             + (wm?0:Z_FLAG) + (wmh&S_FLAG)        ¥
  302.             + (~(HL^R)&(wm^HL)&0x8000?V_FLAG:0);¥
  303.     HL    = wm
  304.    
  305. #define M_SBCW(R)                                ¥
  306.     wm    = HL-R-(rf&C_FLAG);                        ¥
  307.     rf    = (((Long)HL-(Long)R-(rf&C_FLAG))>>31)    ¥
  308.             + (wm?0:Z_FLAG) + (wmh&S_FLAG)        ¥
  309.             + ((HL^R)&(wm^HL)&0x8000?V_FLAG:0)    ¥
  310.             + N_FLAG;                            ¥
  311.     HL    = wm
  312.  
  313. /*    IN    set/clr    Z, P, S, H
  314.         clear    N=0
  315.         pres    C
  316. */
  317. #define M_IN(R)                ¥
  318.     R    = Do_Input(BC);        ¥
  319.     rf    = (rf&C_FLAG) + zlog_flags[R]
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.